<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="Matlab入门(一), 十二惊惶的gitee">
    <meta name="description" content="Matlab入门（一）[TOC]
1.Matlab系统环境Matlab主窗口：
功能区：提供三个选项卡（主页，绘图，应用程序），各自有不同的工具可供使用；快速访问工具栏：包含一些常用按钮；当前文件夹工具栏：用于实现当前文件夹的操作。一定要先">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="referrer" content="no-referrer-when-downgrade">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>Matlab入门(一) | 十二惊惶的gitee</title>
    <link rel="icon" type="image/png" href="/shier_jinghuang/favicon.png">
    


    <!-- bg-cover style     -->



<link rel="stylesheet" type="text/css" href="/shier_jinghuang/libs/awesome/css/all.min.css">
<link rel="stylesheet" type="text/css" href="/shier_jinghuang/libs/materialize/materialize.min.css">
<link rel="stylesheet" type="text/css" href="/shier_jinghuang/libs/aos/aos.css">
<link rel="stylesheet" type="text/css" href="/shier_jinghuang/libs/animate/animate.min.css">
<link rel="stylesheet" type="text/css" href="/shier_jinghuang/libs/lightGallery/css/lightgallery.min.css">
<link rel="stylesheet" type="text/css" href="/shier_jinghuang/css/matery.css">
<link rel="stylesheet" type="text/css" href="/shier_jinghuang/css/my.css">
<link rel="stylesheet" type="text/css" href="/shier_jinghuang/css/dark.css" media="none" onload="if(media!='all')media='all'">




    <link rel="stylesheet" href="/shier_jinghuang/libs/tocbot/tocbot.css">
    <link rel="stylesheet" href="/shier_jinghuang/css/post.css">




    



    <script src="/shier_jinghuang/libs/jquery/jquery-3.6.0.min.js"></script>

<meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/shier_jinghuang/atom.xml" title="十二惊惶的gitee" type="application/atom+xml">
</head>


<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/shier_jinghuang/" class="waves-effect waves-light">
                    
                    <img src="/shier_jinghuang/medias/1.jpg" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">十二惊惶的gitee</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shier_jinghuang/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shier_jinghuang/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shier_jinghuang/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shier_jinghuang/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shier_jinghuang/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shier_jinghuang/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shier_jinghuang/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
  <li>
    <a href="javascript:;" class="waves-effect waves-light" onclick="switchNightMode()" title="深色/浅色模式" >
      <i id="sum-moon-icon" class="fas fa-sun" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/shier_jinghuang/medias/1.jpg" class="logo-img circle responsive-img">
        
        <div class="logo-name">十二惊惶的gitee</div>
        <div class="logo-desc">
            
            犁牛之子骍且角，虽欲勿用，山川其舍诸
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/shier_jinghuang/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/shier_jinghuang/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/shier_jinghuang/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/shier_jinghuang/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/shier_jinghuang/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/shier_jinghuang/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/shier_jinghuang/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://gitee.com/Im-so-scared-2/shier_jinghuang" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>十二惊惶
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://gitee.com/Im-so-scared-2/shier_jinghuang" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="十二惊惶" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/shier_jinghuang/medias/featureimages/7.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">Matlab入门(一)</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/shier_jinghuang/tags/%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/">
                                <span class="chip bg-color">学习记录</span>
                            </a>
                        
                            <a href="/shier_jinghuang/tags/Matlab/">
                                <span class="chip bg-color">Matlab</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/shier_jinghuang/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" class="post-category">
                                学习笔记
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2022-01-10
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2022-01-14
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    3.9k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    13 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/shier_jinghuang/libs/prism/prism.min.css">
        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="Matlab入门（一）"><a href="#Matlab入门（一）" class="headerlink" title="Matlab入门（一）"></a>Matlab入门（一）</h2><p>[TOC]</p>
<h3 id="1-Matlab系统环境"><a href="#1-Matlab系统环境" class="headerlink" title="1.Matlab系统环境"></a>1.Matlab系统环境</h3><p><strong>Matlab主窗口：</strong></p>
<p><strong>功能区：</strong>提供三个选项卡（主页，绘图，应用程序），各自有不同的工具可供使用；<strong>快速访问工具栏</strong>：包含一些常用按钮；<strong>当前文件夹工具栏</strong>：用于实现当前文件夹的操作。一定要先建立文件再将其设为工作文件夹。</p>
<p><strong>命令行窗口</strong></p>
<p>命令行窗口用于输入命令并显12示命令的执行结果。在命令提示符$&gt;&gt;$后输入命令并按下回车键，Matlab就会解释执行输入的命令并显示结果。如果命令行很长可以在第一个命令行后面加三个小数点…在下一回继续输入命令的其他部分，这三个…称为<strong>续行符</strong>，用于将下一行的命令视为这一行命令的逻辑相连。</p>
<ol>
<li><p>使用path命令设置文件搜索路径。例如：&gt;&gt;path(path,’e:\work’)    </p>
</li>
<li><p>用对话框设置文件搜索路径。</p>
<p>注：当前文件夹与搜索路径文件夹下均有同名m文件，会执行当前文件夹下的m文件。</p>
</li>
</ol>
<p><strong>工作区窗口：</strong> 是Matlab用于存储各种变量和结果的空间，可用于变量的显示和操作。在工作区以表格形式显示变量a与x的名称，取值等信息。在工作区还可以对变量进行保存、编辑、删除等操作。</p>
<p><strong>设置文件搜索路径</strong></p>
<p>MATLAB是在当前文件夹与搜索路径文件夹中寻找这个文件并执行文件内容的，所以要执行的文件必须放在这两类文件夹中。</p>
<ol>
<li><p>使用<code>path命令</code>设置文件搜索路径。例如：&gt;&gt;path(path,’e:\work’)    </p>
</li>
<li><p>用对话框设置文件搜索路径。</p>
<p>注：当前文件夹与搜索路径文件夹下均有同名m文件，会执行当前文件夹下的m文件。</p>
</li>
</ol>
<h3 id="2-Matlab的数值数据"><a href="#2-Matlab的数值数据" class="headerlink" title="2.Matlab的数值数据"></a>2.Matlab的数值数据</h3><p>**整型 : **</p>
<p>无符号整数:无符号8位整数、无符号16位整数、无符号32位整数、无符号64位整数.带符号整数:带符号8位整数、带符号16位整数、带符号32位整数、带符号64位整数。</p>
<p><strong>浮点型：</strong></p>
<p>在使用强制类型转化时，如x=int8(129)此时，x只会被置为int8(带符号数)的最大值127.</p>
<p>例如，uint8函数将数值数据转换为无符号8位整数，int8函数将数值数据转换为带符号8位整数。</p>
<p>浮点型数据分为单精度型和双精度类型，单精度型实数在内存中占用4个字节，而双精度型实数在内存中占用8个字节，所以双精度型的数据精度更高。</p>
<p>single函数:将其他类型的数据转换为单精度型。</p>
<p>double函数:将其他类型的数据转换为双精度型。</p>
<p>class函数可以得到参数的数据类型。</p>
<p>复型数据包括实部和虚部两个部分，实部和虚部默认为双精度型，虚数单位用i或|来表示。</p>
<pre class="line-numbers language-matlab" data-language="matlab"><code class="language-matlab"><span class="token function">real</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">%求复数的实部</span>
<span class="token function">imag</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">%求复数的虚部</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p><strong>数值数据的输出格式</strong></p>
<p>Matlab可以用format函数设置数值数据的输出格式，</p>
<pre class="line-numbers language-matlab" data-language="matlab"><code class="language-matlab">format 格式符  <span class="token comment">% 设置数据的输出格式</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>不同的格式符输出不同的数据格式，同时format命令只是影响数据输出的格式，而不影响数据的计算和存储。</p>
<h4 id="常用数学函数"><a href="#常用数学函数" class="headerlink" title="常用数学函数"></a>常用数学函数</h4><p>（1）函数的调用格式为：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">函数名（函数自变量的值）<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>函数的自变量规定为矩阵变量（标量本身是矩阵的一种特例）。<br><strong>函数在运算时是将函数逐项作用于矩阵的每个元素上</strong>，所以最后运算的结果就是一个与自变量同型的矩阵。</p>
<p>常用函数</p>
<p>三角函数有以弧度为单位的函数和以角度为单位的函数,如果是以角度为单位的函数就在函数名后面加“d”，以示区别。</p>
<p>abs函数可以求实数的绝对值、复数的模、字符串的ASClI码值。</p>
<p>用于取整的函数有fix、floor、ceil、round。round函数按照四舍五入的规则取整。ceil函数是向上取整，取大于等于这个数的第一个整数。floor函数是向下取整，取小于等于这个数的第一个整数。fix函数是固定取靠近0的那个整数,即舍去小数取整。</p>
<pre class="line-numbers language-matlab" data-language="matlab"><code class="language-matlab"><span class="token function">isprime</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span> # 判断n是否是素数，是返回<span class="token number">1</span>，否返回<span class="token number">0.</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h3 id="3-变量及其操作"><a href="#3-变量及其操作" class="headerlink" title="3.变量及其操作"></a>3.变量及其操作</h3><p>计算机处理的数据都是存放在内存单元中的，而每一个内存单元都有一个唯一的地址，程序就是通过这个地址来访问对应内存单元的。<br>在如Matlab这种高级语言中，只需要给每个内存单元取一个名字，然后通过这个名字就能访问每个内存单元了。</p>
<h4 id="变量与赋值语句"><a href="#变量与赋值语句" class="headerlink" title="变量与赋值语句"></a>变量与赋值语句</h4><ul>
<li>变量本质上讲是内存单元的一个抽象</li>
<li>在Matlab中，变量名是以字母开头，后接字母、数字或下划线的字母序列，最多63个字符。</li>
</ul>
<p>赋值语句两种格式</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">变量<span class="token operator">=</span>表达式        % 变量结果会在命令行中显示出来
变量<span class="token operator">=</span>表达式；	  % 变量结果不会在命令行中显示出来<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>Matlab语言还有一些预定义的变量，预定义变量是由系统本身定义的变量。如ans是默认赋值变量，i和j代表虚数单位，pi代表圆周率，NaN代表非数。</p>
<h4 id="变量的删除和修改（who-whos）"><a href="#变量的删除和修改（who-whos）" class="headerlink" title="变量的删除和修改（who,whos）"></a>变量的删除和修改（who,whos）</h4><p>在工作区选择变量右击即可进行删除和修改。<br>who命令和whos命令：<br>who命令只显示已存在变量的名称。而whos显示包括变量类型，长度等信息。</p>
<h4 id="内存变量文件-save-load"><a href="#内存变量文件-save-load" class="headerlink" title="内存变量文件(save,load)"></a>内存变量文件(save,load)</h4><p>用于保存Matlab工作区变量的文件叫做内存变量文件，其扩展名为.mat，也叫MAT文件，是一种标准的二进制格式文件。<br><strong>save命令：创建内存变量文件</strong>                                                               <strong>load命令：载入内存变量文件</strong></p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">save mydata a x   % 将a x保存在mydata.mat文件中
load mydata       % 载入mydata.mat中的变量<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<h3 id="4-MATLAB矩阵表示"><a href="#4-MATLAB矩阵表示" class="headerlink" title="4.MATLAB矩阵表示"></a>4.MATLAB矩阵表示</h3><h4 id="矩阵的建立"><a href="#矩阵的建立" class="headerlink" title="矩阵的建立"></a>矩阵的建立</h4><p><img src="https://shierjinghuang.github.io/2022/01/10/Matlab/1.png"></p>
<p><img src="https://shierjinghuang.github.io/2022/01/10/Matlab/2.png"></p>
<p><img src="https://shierjinghuang.github.io/2022/01/10/Matlab/3.png"></p>
<h4 id="冒号表达式："><a href="#冒号表达式：" class="headerlink" title="冒号表达式："></a>冒号表达式：</h4><p>1 用冒号产生行向量t：</p>
<pre class="line-numbers language-matlab" data-language="matlab"><code class="language-matlab">t<span class="token operator">=</span><span class="token number">0</span><span class="token operator">:</span><span class="token number">1</span><span class="token operator">:</span><span class="token number">5</span> <span class="token comment">%格式为： 初始值：步长：终止值</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>2 用linspace函数产生行向量x:</p>
<pre class="line-numbers language-matlab" data-language="matlab"><code class="language-matlab">x<span class="token operator">=</span><span class="token function">linspace</span><span class="token punctuation">(</span>a<span class="token operator">:</span>b<span class="token operator">:</span>n<span class="token punctuation">)</span><span class="token comment">%(第一个元素：第二个元素：总数)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>如果n忽略不写则自动生成100个数据。</p>
<h4 id="结构矩阵和单元矩阵"><a href="#结构矩阵和单元矩阵" class="headerlink" title="结构矩阵和单元矩阵"></a>结构矩阵和单元矩阵</h4><p><strong>结构矩阵：</strong></p>
<p>   在实际应用中，经常会遇到不同类型的数据项组成的实体，例如一个学生的数据实体就包括了年龄，姓名，学号，这些数据是相互联系的。结构数据类型就可以把这些数据类型不同但逻辑上相关的数据组成一个有机的整体。</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">结构矩阵元素.成员名<span class="token operator">=</span>表达式<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>单元矩阵：</strong></p>
<p>单元数据类型，也是把不同的数据放在一个变量中，但与结构矩阵所不同的是，结构矩阵的各个元素下还有成员，每个成员都有自己的名字，而单元矩阵的各个元素就是不同类型的数据，每个元素直接由不同类型的数据组成。</p>
<h3 id="5-矩阵元素的引用"><a href="#5-矩阵元素的引用" class="headerlink" title="5.矩阵元素的引用"></a>5.矩阵元素的引用</h3><p><strong>1 通过下标来引用矩阵的元素</strong></p>
<p><strong>2 通过序号来引用：</strong>在MATLAB中，矩阵元素按列存储，即首先存储矩阵的第一列元素，然后存储第二列元 素，一直到矩阵的最后一列元素。矩阵元素的序号就是矩阵元素在内存中的排列顺序。</p>
<p> 序号与下标是一—对应的，以m×n矩阵A为例，矩阵元素A(i.j)的序号为(j-1)×m+i。矩阵元素的序号与下标可以利用sub2ind和ind2sub函数实现相互转换。<br>sub2ind函数:将矩阵中指定元素的行、列下标转换成存储的序号。调用格式为:</p>
<pre class="line-numbers language-matlab" data-language="matlab"><code class="language-matlab">D<span class="token operator">=</span><span class="token function">sub2ind</span><span class="token punctuation">(</span>S<span class="token punctuation">,</span>I<span class="token punctuation">,</span>J<span class="token punctuation">)</span><span class="token comment">%  S行数和列数组成的向量  I转换矩阵元素的行下标  J转换矩阵元素的列下标</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>ind2ind函数:将矩阵元素的序号转换成对应的下标，调用格式为:</p>
<pre class="line-numbers language-matlab" data-language="matlab"><code class="language-matlab"><span class="token punctuation">[</span>I<span class="token punctuation">,</span>J<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">ind2sub</span><span class="token punctuation">(</span>S<span class="token punctuation">,</span>D<span class="token punctuation">)</span><span class="token comment">% I行下标 J列下标 S行数和列数组成的向量 D序号</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>3 利用冒号表达式获得子矩阵(end)</strong></p>
<p>子矩阵是指由矩阵中一部分元素构成的矩阵。</p>
<p><img src="https://shierjinghuang.github.io/2022/01/10/Matlab/4.png"></p>
<p>end运算符：表示某一维的末尾元素下标</p>
<p>可以利用空矩阵删除矩阵的元素，</p>
<p><strong>4.改变矩阵的形状(reshape)</strong></p>
<p>在矩阵<strong>总元素保持不变</strong>的前提下，将矩阵A重新排成$m*n$的二维矩阵。</p>
<pre class="line-numbers language-matlab" data-language="matlab"><code class="language-matlab"><span class="token function">reshape</span> <span class="token punctuation">(</span>A<span class="token punctuation">,</span>m<span class="token punctuation">,</span>n<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>注意：reshape函数只是改变原矩阵的行数和列数，但并不改变原矩阵元素个数及其存储顺序。</p>
<p>A(:)将矩阵A的每一列元素堆叠起来成为一个列向量。</p>
<pre class="line-numbers language-matlab" data-language="matlab"><code class="language-matlab"><span class="token function">A</span><span class="token punctuation">(</span><span class="token operator">:</span><span class="token punctuation">)</span>等价于 <span class="token function">reshape</span><span class="token punctuation">(</span>A<span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h3 id="6-Matlab基本运算"><a href="#6-Matlab基本运算" class="headerlink" title="6.Matlab基本运算"></a>6.Matlab基本运算</h3><h4 id="6-1算数运算"><a href="#6-1算数运算" class="headerlink" title="6.1算数运算"></a>6.1算数运算</h4><h5 id="基本算数运算"><a href="#基本算数运算" class="headerlink" title="基本算数运算"></a>基本算数运算</h5><p> 基本算术运算符:<strong>＋（加）、—（减）、*（乘）</strong> <strong>/（左除） \（右除）^乘方</strong><br>MATLAB的算术运算是在矩阵意义下进行的。单个数据的算术运算只是矩阵运算的一种特例。</p>
<p>**加减运算:**若两矩阵同型，则运算时两矩阵的相应元素相加减。若两矩阵不同型，则MATLAB将给出错误信息,一个标量也可以和矩阵进行加减运算，这时把标量和矩阵的每一个元素进行加减运算. </p>
<p><strong>乘法运算：</strong>矩阵A和B进行乘法运算，要求A的列数与B的行数相等，此时则称A、B矩阵是可乘的，或称A和B两矩阵维数和大小相容。如果两者的维数或大小不相容，则将给出错误信息，提示用户两个矩阵是不可乘的。 </p>
<p><strong>除法运算：</strong>在<strong>MATLAB</strong>中，有两种矩阵除法运算:右除/和左除\。如果A矩阵是非奇异方阵，则B/A等效于B * inv(A)，A\B等效于inv(A) * B。对于矩阵来说，右除和左除表示两种不同的除数矩阵和被除数矩阵关系。</p>
<pre class="line-numbers language-none"><code class="language-none">左除： A &#x2F; B &#x3D; A * B ^ -1
右除： A \ B &#x3D; A ^ -1 * B<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p><strong>乘方运算：</strong>一个矩阵的乘方运算可以表示成A^x，要求A为方阵，x为标量。</p>
<h5 id="点运算"><a href="#点运算" class="headerlink" title="点运算"></a>点运算</h5><p>**点运算:**两矩阵进行点运算是指它们的对应元素进行相关运算，要求两矩阵同型。Matlab中的点运算，就是对矩阵中的元素一一运算，基本的点运算有点乘、点除、点幂。</p>
<p><img src="https://shierjinghuang.github.io/2022/01/10/Matlab/5.png"></p>
<center>（点运算与算数运算的区别）</center>
.* 点乘 ：将每个元素乘以一个数。

<p>./ (左点除) ：将每个元素除以一个数或者矩阵。如A ./ B A,B对应元素相除。(在点除的时候需要保证矩阵形式相同)</p>
<p>.\ (右点除) : </p>
<pre class="line-numbers language-matlab" data-language="matlab"><code class="language-matlab">A<span class="token operator">.\</span><span class="token number">3</span> <span class="token comment">%3除以A中对应元素</span>
A<span class="token operator">.\</span> B<span class="token comment">%用每一个元素分别对应除以矩阵中的元素。</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>.^(点幂) ：</p>
<pre class="line-numbers language-matlab" data-language="matlab"><code class="language-matlab">A<span class="token operator">.^</span><span class="token number">1.2</span> <span class="token comment">%A中每个元素的1.2次方</span>
A<span class="token operator">.^</span>B   <span class="token comment">%A中每个元素作为底数，B中对应元素作为次数，进行幂运算</span>
<span class="token number">3.</span><span class="token operator">^</span>B   <span class="token comment">%以3为底、B中元素为次数</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>Matlab用%表示注释。</p>
<h4 id="6-2-关系运算"><a href="#6-2-关系运算" class="headerlink" title="6.2 关系运算:"></a>6.2 关系运算:</h4><p>&lt;(小于)、&lt;=(小于或等于)、&gt;(大于)、&gt;=(大于或等于)、==(等于)、~=(不等于)。<br>当两个比较量是标量时，直接比较两数的大小。若关系成立，关系表达式结果为1，否则为0。<br>当参与比较的量是两个同型的矩阵时，比较是对两矩阵相同位置的元素按标量关系运算规则逐个进行，最终的关系运算的结果是一个与原矩阵同型的矩阵，它的元素由0或1组成。当参与比较的一个是标量，而另一个是矩阵时则把标量与矩阵的每一个元素按标量关系运算规则逐个比较，最终的关系运算的结果是一个与原矩阵同型的矩阵，它的元素由0或1组成。</p>
<h4 id="6-3-逻辑运算"><a href="#6-3-逻辑运算" class="headerlink" title="6.3 逻辑运算"></a>6.3 逻辑运算</h4><p>:&amp;(与)、I(或)和～(非)。<br>设参与逻辑运算的是两个标量a和b，那么运算规则为:a&amp;b a、b全为非零时，运算结果为1，否则为0。alb a、b中只要有一个为非零时，运算结果为1。~a当a为零时，运算结果为1;当a为非零时，运算结果为0。</p>
<p> 在算术运算、关系运算和逻辑运算中，算术运算的优先级最高，逻辑运算优先级最低，但逻辑非运算是单目运算，它的优先级比双目运算要高。<strong>单目是涉及一个元素的运算，双目是涉及两个的运算</strong>。<br>若参与逻辑运算的是两个同型矩阵，那么将对矩阵相同位置上的元素按标量规则逐个进行运算，最终运算结果是一个与原矩阵同型的矩阵，其元素由1或0组成。若参与逻辑运算的一个是标量，一个是矩阵，那么将在标量与矩阵中的每个元素之间按标量规则逐个进行运算，最终运算结果是一个与原矩阵同型的矩阵,其元素由1或0组成。</p>
<h3 id="7-Matlab中的-m文件"><a href="#7-Matlab中的-m文件" class="headerlink" title="7.Matlab中的.m文件"></a>7.Matlab中的.m文件</h3><p>.m文件分为两类 </p>
<ol>
<li><p>脚本文件，不传参数只执行计算等功能。</p>
<p>脚本文件只是一个计算模块，保存文件名时可以在满足命名规则的基础上任意命名。</p>
</li>
<li><p>函数文件，需要传参数，返回计算结果。</p>
<p>函数文件必须以关键字function开头，一般需要传参数和返回值，在保存是必须以函数名作为文件名保存。</p>
<p>注意在调用自己编写的函数时，调用的函数与被调用的函数必须放在同一文件夹下</p>
</li>
<li><p>脚本文件和函数文件可以相互转换</p>
</li>
</ol>
<h3 id="8-Matlab中的基本语句"><a href="#8-Matlab中的基本语句" class="headerlink" title="8.Matlab中的基本语句"></a>8.Matlab中的基本语句</h3><h4 id="条件语句"><a href="#条件语句" class="headerlink" title="条件语句"></a>条件语句</h4><ol>
<li>格式1 if end条件语句</li>
<li>格式2 if else end 条件语句</li>
<li>格式3 if else elseif end 条件语句</li>
<li>switch语句，适用于有更多选项的程序。</li>
<li>try语句try语句尝试执行语句1，如果在执行过程中出现错误，则转去执行语句组2.</li>
</ol>
<h4 id="循环语句"><a href="#循环语句" class="headerlink" title="循环语句"></a>循环语句</h4><ol>
<li>for-end循环，for循环变量由初值，步长和终值决定，内部为循环体语句。for语句更一般的格式为 for 循环变量=矩阵表达式，然后执行循环体语句直到各列元素处理完毕。</li>
</ol>
<p>蒙特卡罗方法求概率，用随机数模拟实际过程的方法。</p>
<p>二分法求根的编程思路1,定义求根的函数2,主函数使用while循环比较合适。</p>
<p>Matlab中的数组下标是从1开始的。</p>
<p>Matlab以矩阵或向量为元素进行运算，可以减少循环，从而减少运行时间,点运算是Matlab的特点</p>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/shier_jinghuang/about" rel="external nofollow noreferrer">十二惊惶</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://im-so-scared-2.gitee.io/shier_jinghuang/shier_jinghuang/2022/01/10/Matlab/">https://im-so-scared-2.gitee.io/shier_jinghuang/shier_jinghuang/2022/01/10/Matlab/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/shier_jinghuang/about" target="_blank">十二惊惶</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/shier_jinghuang/tags/%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/">
                                    <span class="chip bg-color">学习记录</span>
                                </a>
                            
                                <a href="/shier_jinghuang/tags/Matlab/">
                                    <span class="chip bg-color">Matlab</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/shier_jinghuang/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/shier_jinghuang/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/shier_jinghuang/2022/01/12/Matlab%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86/">
                    <div class="card-image">
                        
                        
                        <img src="/shier_jinghuang/medias/featureimages/3.jpg" class="responsive-img" alt="Matlab数据处理">
                        
                        <span class="card-title">Matlab数据处理</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2022-01-12
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/shier_jinghuang/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" class="post-category">
                                    学习笔记
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/shier_jinghuang/tags/%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/">
                        <span class="chip bg-color">学习记录</span>
                    </a>
                    
                    <a href="/shier_jinghuang/tags/Matlab/">
                        <span class="chip bg-color">Matlab</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/shier_jinghuang/2021/12/09/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E7%AC%AC%E4%B8%89%E7%AB%A0%E6%A0%88%E5%92%8C%E9%98%9F%E5%88%97/">
                    <div class="card-image">
                        
                        
                        <img src="/shier_jinghuang/medias/featureimages/6.jpg" class="responsive-img" alt="数据结构 第三章栈和队列">
                        
                        <span class="card-title">数据结构 第三章栈和队列</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2021-12-09
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            十二惊惶
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/shier_jinghuang/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">
                        <span class="chip bg-color">数据结构</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/shier_jinghuang/libs/codeBlock/codeBlockFuction.js"></script>


  <!-- 是否加载使用自带的 prismjs. -->
  <script type="text/javascript" src="/shier_jinghuang/libs/prism/prism.min.js"></script>


<!-- 代码语言 -->

<script type="text/javascript" src="/shier_jinghuang/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/shier_jinghuang/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/shier_jinghuang/libs/codeBlock/codeShrink.js"></script>



    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/shier_jinghuang/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/shier_jinghuang/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="503838841"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/shier_jinghuang/libs/aplayer/APlayer.min.js"></script>
<script src="/shier_jinghuang/libs/aplayer/Meting.min.js"></script>

    

    <div class="container row center-align"
         style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2019-2023</span>
            
            <a href="/shier_jinghuang/about" target="_blank">十二惊惶</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
                &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                        class="white-color">289.6k</span>
            
            
            
                
            
            
                <span id="busuanzi_container_site_pv">
                &nbsp;|&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;
                    <span id="busuanzi_value_site_pv" class="white-color"></span>
            </span>
            
            
                <span id="busuanzi_container_site_uv">
                &nbsp;|&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;
                    <span id="busuanzi_value_site_uv" class="white-color"></span>
            </span>
            
            <br>

            <!-- 运行天数提醒. -->
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/blinkfox" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:1181062873@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=1181062873" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 1181062873" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







    <a href="/shier_jinghuang/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/shier_jinghuang/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 白天和黑夜主题 -->
<div class="stars-con">
    <div id="stars"></div>
    <div id="stars2"></div>
    <div id="stars3"></div>  
</div>

<script>
    function switchNightMode() {
        $('<div class="Cuteen_DarkSky"><div class="Cuteen_DarkPlanet"></div></div>').appendTo($('body')),
        setTimeout(function () {
            $('body').hasClass('DarkMode') 
            ? ($('body').removeClass('DarkMode'), localStorage.setItem('isDark', '0'), $('#sum-moon-icon').removeClass("fa-sun").addClass('fa-moon')) 
            : ($('body').addClass('DarkMode'), localStorage.setItem('isDark', '1'), $('#sum-moon-icon').addClass("fa-sun").removeClass('fa-moon')),
            
            setTimeout(function () {
            $('.Cuteen_DarkSky').fadeOut(1e3, function () {
                $(this).remove()
            })
            }, 2e3)
        })
    }
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/shier_jinghuang/libs/materialize/materialize.min.js"></script>
    <script src="/shier_jinghuang/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/shier_jinghuang/libs/aos/aos.js"></script>
    <script src="/shier_jinghuang/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/shier_jinghuang/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/shier_jinghuang/js/matery.js"></script>

    

    

    <!-- 雪花特效 -->
    

    <!-- 鼠标星星特效 -->
    

     
        <script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
        <script src="/shier_jinghuang/libs/others/TencentCaptcha.js"></script>
        <button id="TencentCaptcha" data-appid="xxxxxxxxxx" data-cbfn="callback" type="button" hidden></button>
    

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/shier_jinghuang/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/shier_jinghuang/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/shier_jinghuang/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
